/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode *p = new ListNode(0, head), *q = head;
        ListNode *temp = p;
        if(!q)return q;
        while(q && q->next){
            if(q->val == q->next->val){
                int repeat = q->val;
                while(q && q->val == repeat){
                    p->next = q->next;
                    q = q->next;
                }
            }
            else {
                p = p->next;
                q = q->next;
            }
        }
        return temp->next;
    }
};

//2021.3.25 
